package com.heiye.blog.ai.model.dto;

import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Lists;
import com.heiye.blog.ai.model.vo.AIRoleChatReqVO;
import com.heiye.blog.ai.model.vo.AiChatReqVO;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.ai.chat.client.advisor.api.Advisor;

import java.util.Collections;
import java.util.List;

/**
 * @author: heiye
 * @date: 2025/11/19 下午3:54
 * @version: v1.0.0
 * @description: 聊天请求构建
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AIChatRequest {
    /**
     * 对话 ID
     */
    private String chatId;

    /**
     * 用户名称
     */
    private String username;

    /**
     * 模型名称
     */
    private String textModelName;

    /**
     * 联网搜索
     */
    private Boolean networkSearch = false;

    /**
     * 此设置会影响模型响应的多样性。较低的值会导致更可预测和典型的响应，
     * 而较高的值则鼓励更多样化和不太常见的响应。
     * 在 0 时，模型始终对给定输入给出相同的响应。
     * Range: [0, 2]
     */
    private Double temperature;

    /**
     * 是否流式返回结果
     */
    private Boolean stream;

    /**
     * 用户消息
     */
    private String userMessage;

    /**
     * Advisor 集合
     */
    private List<Advisor> advisors;

    /**
     * 语言
     */
    private String language;

    /**
     * 深度思考
     */
    private Boolean thinkEnable = false;

    private Boolean incrementalOutput = true;

    /**
     * 分配多少 token 用于推理
     */
    private String effort;

    /**
     * 推理分配最大 token 值
     */
    private Integer thinkMaxTokens = 0;

    /**
     * 是否排除推理结果
     */
    private Boolean exclude;

    /**
     * 添加 Advisor
     *
     * @param advisor
     * @return
     */
    public AIChatRequest advisor(Advisor advisor) {
        // 添加 Advisor
        if (CollUtil.isNotEmpty(this.advisors)) {
            this.advisors.add(advisor);
        } else {
            this.advisors = Lists.newArrayList(advisor);
        }
        return this;
    }

    /**
     * 使用 aiChatReqVO 构建 AIChatRequest
     *
     * @param aiChatReqVO
     */
    public AIChatRequest(AiChatReqVO aiChatReqVO) {
        this.chatId = aiChatReqVO.getChatId();
        this.textModelName = aiChatReqVO.getModelName();
        this.userMessage = aiChatReqVO.getMessage();
        this.networkSearch = aiChatReqVO.getNetworkSearch();
        this.thinkEnable = aiChatReqVO.getThinkEnable();
    }

    /**
     * 使用 aiRoleChatReqVO 构建 AIChatRequest
     *
     * @param aiRoleChatReqVO
     */
    public AIChatRequest(AIRoleChatReqVO aiRoleChatReqVO) {
        this.chatId = aiRoleChatReqVO.getChatId();
        this.username = aiRoleChatReqVO.getUsername();
        this.textModelName = aiRoleChatReqVO.getTextModelName();
        this.userMessage = aiRoleChatReqVO.getMessage();
        this.language = aiRoleChatReqVO.getLanguage();
    }
}
